// pages/classify/classify.js
import { nine, category, productdetails, addshop, categorys } from '../../utils/http'
Page({
    data: {
        activeKey: null,
        nine: [],
        id: '',
        list: [],
        value: '',
        // 商品列表
        goods: [],
        ids: null
    },
    onLoad: function (options) {
        console.log(options.id)
        //搜索
        categorys().then(res => {
            console.log(res)
            let good = res.data
            this.setData({ goods: good,ids:options.id})
        })
        this.setData({ activeKey: options.index })

        wx.setNavigationBarTitle({
            title: '分类'
        })
        category(187437).then(res => {
            if (res.msg == 'success') {
                this.setData({ list: res.data })
            }
        })
        // 九宫格
        nine().then(res => {
           
            this.setData({ nine: res.data})
            category(this.data.ids).then(res => {
                if (res.msg == 'success') {
                    this.setData({ list: res.data })
                }
            })
        })


    },
    right: function (e) {
        let id = e.currentTarget.dataset.id
        console.log(id)
        //右边数据
        category(this.data.ids).then(res => {
            if (res.msg == 'success') {
                this.setData({ list: res.data, activeId: id })
            } else {
                this.setData({ list: [] })
            }
        })
    },

    addToCar(e) {
        let id = e.currentTarget.dataset.id
        let number = 1
        let token = wx.getStorageSync('token')
        productdetails(id).then(res => {
            console.log(res.data.pics[0].goodsId)
            let goodsId = res.data.pics[0].goodsId

            addshop(token, goodsId, number).then(res => {
                let data = JSON.stringify(res.data)
                wx.reLaunch({
                    url: '/pages/goods/goods?list=' + data
                })
            })
        })
    },
    searchChangeHandle(e) {
        console.log(e.detail)
        this.setData({ value: e.detail })
    },
    searchHandle() {
        let good = this.data.goods
        let goods = JSON.stringify(good)
        let val = this.data.value
        console.log(val)
        wx.reLaunch({
            url: "/pages/goodslist/goodslist?goods=" + goods + `&val=` + val,
        })

    },

})